<template>
	<view>
		<view class="addres_box">
			<view class="inputs">
				<label>姓名</label> <input type="text" placeholder="请输入姓名" v-model="name" />
			</view>
			<view class="inputs">
				<label>手机号</label> <input type="text" placeholder="请输入手机号" v-model="phone" />
			</view>

		</view>
		<view class="shoplist">
			<view class="shoplist_item" v-for="(item,index) in cartinfo.cartInfo" :key="index">
				<view class="shop_img">
					<u-image width="140rpx" height="140rpx" :src="item.productInfo.image"></u-image>
				</view>
				<view class="shopval">
					<view class="shop_name">
						{{item.productInfo.store_name}}
					</view>
					<view class="shop_xinghao">
						{{item.productInfo.attrInfo.suk?item.productInfo.attrInfo.suk:''}}
					</view>
					<view class="shop_num">
						x{{item.cart_num}}
					</view>
				</view>
				<view class="shop_jiage">
					<text class="yuanjia_icon">FCFA</text><text class="yuanjia">{{item.productInfo.price}}</text>
					<text class="xianjia_icon">FCFA</text><text class="xianjia">{{item.productInfo.truePrice}}</text>
				</view>
			</view>
		</view>
		<view class="youhuiquan" @click="xuanzeyhq">
			<view class="yhqLeft">
				满减优惠卷
			</view>
			<view class="yhqRight">
				<view class="yhqval" v-if="usableCoupon.coupon_price">
					{{usableCoupon.coupon_price}}
				</view>
				<view class="yhqval" v-else>
					无
				</view>
				<view class="yhqIcon">
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<view class="updingdan">
			<view class="upleft">
				<text class="heji">合计:</text>
				<text class="hejijiage">FCFA
					<text
						class="hejijiage">{{ usableCoupon.coupon_price ? cartinfo.priceGroup.totalPrice - usableCoupon.coupon_price : cartinfo.priceGroup.totalPrice}}</text>
				</text>
			</view>
			<view class="tijiao" @click="tijiaoFun">
				提交订单
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: 'https://cdn.uviewui.com/uview/example/fade.jpg',
				info: null,
				dingdanId: 13528,
				cartinfo: null,
				name: '',
				phone: '',
				first: false,
				usableCoupon: null
			};
		},
		onLoad(data) {
			this.dingdanId = data.id
			uni.getStorage({
				key: 'userinfo',
				success: (res) => {
					this.first = true
					this.info = res.data
					this.gwcjiesuan()
					this.getaddres()
				}
			})
		},
		onShow() {
			console.log('show');
			if (this.first) {
				uni.getStorage({
					key: 'addres',
					success: (res) => {
						this.addres = res.data
						uni.removeStorage({
							key: 'addres'
						})
					}
				})
				uni.getStorage({
					key: 'youhuiquan',
					success: (res) => {
						this.usableCoupon = res.data
						console.log(this.usableCoupon);
						uni.removeStorage({
							key: 'youhuiquan'
						})
					},
				})
			}
		},
		methods: {
			selectaddres() {
				uni.navigateTo({
					url: '../wodedizhi/wodedizhi?xz=' + 0
				})
			},
			xuanzeyhq() {
				uni.navigateTo({
					url: '../youhuiquan/youhuiquan?xz=' + 0
				})
			},
			getaddres() {
				uni.request({
					url: this.$AJAX_URL + 'my/useraddr',
					method: 'POST',
					data: {
						uid: this.info.uid
					},
					success: (res) => {
						console.log(res);
						this.addres = res.data.data[0]
						uni.setStorage({
							key: 'addres',
							data: 'aa'
						})
					}
				})
			},
			gwcjiesuan(id) {
				uni.request({
					url: this.$AJAX_URL + 'order/confirm_order',
					method: 'POST',
					data: {
						uid: this.info.uid,
						cart_id: this.dingdanId
					},
					success: (res) => {
						console.log('2222',res.data.data);
						this.cartinfo = res.data.data
						this.usableCoupon = res.data.data.usableCoupon
					}
				})
			},

			tijiaoFun() {
				console.log(this.phone);
				console.log(this.name);
				if (this.name != '' && this.phone != '') {
					if (this.usableCoupon) {
						uni.request({
							url: this.$AJAX_URL + 'order/create_order',
							method: 'POST',
							data: {
								uid: this.info.uid,
								key: this.cartinfo.orderKey,
								real_name: this.name,
								phone: this.phone,
								address: ' ',
								sheng: '2',
								shi: '2',
								qu: '2',
								couponId: this.usableCoupon.id,
								mark: ''
							},
							success: (res) => {
								console.log('创建订单后的参数', res);
								uni.request({
									url: this.$AJAX_URL + 'order/pay_order',
									method: 'POST',
									data: {
										uid: this.info.uid,
										order_id: res.data.data.result.orderId
									},
									success: (msg) => {
										console.log(msg);
										if (msg.data.code == 400) {
											var data = msg.data.data.result.jsConfig
											uni.requestPayment({
												provider: 'wxpay',
												timeStamp: data.timeStamp,
												nonceStr: data.nonceStr,
												package: data.package,
												paySign: data.paySign,
												signType: "MD5",
												success: (ress) => {
													console.log('我走到了这列');
													console.log(ress);
													if (ress.errMsg ===
														"requestPayment:ok") {
														uni.request({
															url: this
																.$AJAX_URL +
																'order/pay_success',
															method: 'POST',
															data: {
																uid: this.info
																	.uid,
																order_id: res
																	.data.data
																	.result
																	.orderId
															},
															success: (
																resss) => {
																// this.$u
																// 	.toast(
																// 		'支付成功'
																// 		)
																setTimeout(
																	() => {
																		uni.reLaunch({
																			url: '../index/index'
																		})
																	},
																	1500
																)
															}
														})
													} else {
														this.$u.toast('支付失败')
													}
												},
												fail: (err) => {
													console.log(err);
													this.$u.toast('支付失败')
												}
											})
										} else {
											this.$u.toast(msg.data.msg)
										}
									}
								})
							}
						})
					} else {
						uni.request({
							url: this.$AJAX_URL + 'order/create_order',
							method: 'POST',
							data: {
								uid: this.info.uid,
								key: this.cartinfo.orderKey,
								real_name: this.name,
								phone: this.phone,
								address: ' ',
								sheng: '2',
								shi: '2',
								qu: '2',
								mark: ''
							},
							success: (res) => {
								console.log('创建订单后的参数', res.data.data);
								// uni.showToast({
								// 	title: '购买成功',
								// 	icon:'none',
								// 	duration: 2000
								// });
								console.log(res);
								uni.request({
									url: this.$AJAX_URL + 'order/pay_order',
									method: 'POST',
									data: {
										uid: this.info.uid,
										order_id: res.data.data.result.orderId
									},
									success: (msg) => {
										console.log(msg);
										if (msg.data.code == 200) {
											var data = msg.data.data.result.jsConfig
											uni.requestPayment({
												provider: 'wxpay',
												timeStamp: data.timeStamp,
												nonceStr: data.nonceStr,
												package: data.package,
												paySign: data.paySign,
												signType: "MD5",
												success: (ress) => {
													console.log(ress);
													if (ress.errMsg ===
														"requestPayment:ok") {
														uni.request({
															url: this
																.$AJAX_URL +
																'order/pay_success',
															method: 'POST',
															data: {
																uid: this.info
																	.uid,
																order_id: res
																	.data.data
																	.result
																	.orderId
															},
															success: (
																resss) => {
																console
																	.log(
																		resss
																	);
																this.$u
																	.toast(
																		'支付成功'
																	)
																setTimeout(
																	() => {
																		uni.reLaunch({
																			url: '../index/index'
																		})
																	},
																	1500
																)
															}
														})
													} else {
														this.$u.toast('支付失败')
													}
												},
												fail: (err) => {
													console.log(err);
													this.$u.toast('支付失败')
												}
											})
										} else {
											this.$u.toast(msg.data.msg)
											setTimeout(function() {
												uni.reLaunch({
													url: '../daishouhuo/daishouhuo?id=' +
														res.data.data.result
														.orderId
												})
											}, 1500)
										}
									}
								})
							}
						})
					}
				} else {
					uni.showToast({
						title: '请将信息填写完整',
						icon: 'none',
						duration: 2000
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
	}

	.addres_box {
		height: 200rpx;
		margin-top: 20rpx;
		overflow: hidden;
		position: relative;

		.inputs {
			background: #FFFFFF;
			padding: 25rpx;
			display: flex;
			margin-bottom: 3rpx;
			align-items: center;
			justify-content: space-between;

			label {
				font-size: 34rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #000;
			}

			input {
				text-align: right;
			}
		}


	}

	.shoplist {
		padding: 0 20rpx;
		margin-top: 10rpx;
		background: #FFFFFF;

		.shoplist_item {
			display: flex;
			height: 200rpx;
			padding-top: 30rpx;

			.shop_img {
				width: 140rpx;
				height: 140rpx;
				overflow: hidden;
				border-radius: 10rpx;
			}

			.shopval {
				width: 382rpx;
				padding-left: 20rpx;

				.shop_name {
					width: 280rpx;
					font-size: 32rpx;
					color: #010101;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.shop_xinghao {
					font-size: 24rpx;
					color: #010101;
					margin-top: 10rpx;
				}

				.shop_num {
					font-size: 24rpx;
					color: #919191;
					margin-top: 20rpx;
				}
			}

			.shop_jiage {
				.yuanjia_icon {
					color: #999999;
					font-size: 24rpx;
				}

				.yuanjia {
					color: #999999;
					font-size: 32rpx;
					text-decoration: line-through;
				}

				.xianjia_icon {
					font-size: 24rpx;
					margin-left: 25rpx;
				}

				.xianjia {
					font-size: 32rpx;
				}
			}
		}
	}

	.youhuiquan {
		padding-left: 40rpx;
		padding-right: 30rpx;
		height: 100rpx;
		background: #FFFFFF;
		margin-top: 10rpx;
		margin-bottom: 110rpx;

		view {
			display: inline-block;
			line-height: 100rpx;
			font-size: 28rpx;
		}

		.yhqLeft {
			float: left;


			color: #010101;
		}

		.yhqRight {
			float: right;
			color: #999999;

			.yhqval {
				margin-right: 35rpx;
			}
		}
	}

	.updingdan {
		display: flex;
		position: fixed;
		bottom: 0;
		height: 98rpx;
		width: 100%;

		.upleft {
			width: 500rpx;
			background: #FFFFFF;
			line-height: 98rpx;
			text-align: right;
			font-size: 28rpx;
			padding-right: 65rpx;

			.heji {
				color: #010101;
			}

			.hejijiage {
				color: #FF392B;
				margin-left: 20rpx;
			}
		}

		.tijiao {
			width: 250rpx;
			height: 98rpx;
			background: #FF423F;
			text-align: center;
			line-height: 98rpx;
			color: #FFFFFF;
			font-size: 32rpx;

		}
	}
</style>
